package com.budwk.app.sqfwV2.generator.weiqingmen;

import cn.hutool.core.date.DateUtil;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CommonParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.weiqingmen.SafetyCheckDTO;
import com.budwk.app.util.ClassUtil;
import com.budwk.app.util.poiUtils.PictureUtils;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.xwpf.NiceXWPFDocument;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-04-01
 * <p>
 * 威清门消防安全排查 SafetyCheck
 * 威清门街道办事处应急巡查台账（重点场所）.docx
 */
public class KeyPlacePdfGenerator extends BasePdfGenerator {

    public static final List<String> NAMES = Arrays.asList("重点场所（酒店旅馆、餐饮饭店）","重点场所（医院、养老院、学校、幼儿园）","其他重点场所");

    /**
     * 判断使用使用这个generator生成pdf
     *
     * @param pdfDTO 创建pdf数据素材
     * @return 结果
     */
    @Override
    public boolean pdfCreateConditionCheck(PdfGenerateParam pdfDTO) {
        SafetyCheckDTO fieldDTO = (SafetyCheckDTO) pdfDTO.getAbstractCustomerPdfCreateField();
        boolean createFlag = false;
        if (KeyPlacePdfGenerator.NAMES.contains(fieldDTO.getAttributesOfInspectionUnits())) {
            createFlag = true;

        }
        return createFlag;

    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return "sqfwV2/weiqingmen/KeyPlace.docx";
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        // 在这里对一些数据进行填充 进行复杂的业务代码逻辑
        SafetyCheckDTO fieldDTO = (SafetyCheckDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();
        paramMap.put(ClassUtil.getField(SafetyCheckDTO::getInspectionDate),
                fieldDTO.getInspectionDate() == null ? "" : DateUtil.format(fieldDTO.getInspectionDate(), "yyyy-MM-dd"));

        // 巡查问题
        paramMap.put("hiddenDangers", BaseGenerator.getHiddenDangers(fieldDTO));

        //设置门头图片
        BaseGenerator.setDoorPicture(fieldDTO, paramMap);
    }

    /**
     * 设置页脚
     * @param pdfGenerateParam
     * @return
     */
    protected Map<String, Object> createFooterMap(PdfGenerateParam pdfGenerateParam) {
        return BaseGenerator.getFooter(pdfGenerateParam.getCreateUserCommonParam());
    }

    /**
     * 签名、盖章
     * @param xwpfTemplate         XWPDF文件
     * @param pdfGenerateParam 生成pdf的参数
     */
    protected void createXWPDFTemplateAfter(XWPFTemplate xwpfTemplate, PdfGenerateParam pdfGenerateParam) {
        CommonParam createUserCommonParam = pdfGenerateParam.getCreateUserCommonParam();
        SafetyCheckDTO fieldDTO = (SafetyCheckDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();

        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.localImageSeal(xwpfDocument, "※", PictureUtils.getNewLocalImagePath(createUserCommonParam.getLevelCityUnitName()),
                    280, -53, 115);
            PictureUtils.networkImageSeal(xwpfDocument, "签收人", fieldDTO.getCheckedUnitLeader(),
                    50, -5, 65);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
